package com.hesicare.health.worker;

import com.hesicare.adapter.entity.HisDeptLevel;
import com.hesicare.health.entity.SysDept;
import com.hesicare.health.entity.SysUserDept;
import com.hesicare.health.service.IHisDeptLevelService;
import com.hesicare.health.service.ISysDeptService;
import com.hesicare.health.service.ISysUserDeptService;
import com.hesicare.health.service.ISysUserService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class DoUserDept {

    @Autowired
    private ISysUserService ISysUserService;

    @Autowired
    private ISysUserDeptService ISysUserDeptService;

    @Autowired
    private ISysDeptService ISysDeptService;

    @Autowired
    private IHisDeptLevelService IHisDeptLevelService;

    /**
     * 查询His_code 不为空的 部门信息
     */
    public Map<String, Long> getAllDept() {
        Map<String, Long> deptMap = new HashedMap();
        List<SysDept> sysDeptList = ISysDeptService.getAllDept();
        if (!sysDeptList.isEmpty() && sysDeptList != null) {
            deptMap = sysDeptList.stream().collect(Collectors.toMap(sysDept -> (String) sysDept.getHis_code(), SysDept::getDept_id));
        }
        return deptMap;
    }

    public void insertUserDepts(List<SysUserDept> sysUserDept) {
        if (!sysUserDept.isEmpty() && sysUserDept != null) {
            ISysUserDeptService.insertSysUserDept(sysUserDept);
        }
    }

    public void insertUserDept(SysUserDept sysUserDept) {
        if (sysUserDept != null) {
            ISysUserDeptService.insertSysUserDept(sysUserDept);
        }
    }

    public boolean selectByUserDept(SysUserDept sysUserDept) {
        SysUserDept userDept = ISysUserDeptService.getSysUserDept(sysUserDept);
        if (userDept != null) {
            return true;
        } else {
            return false;
        }
    }

    public Map<String, String> getAllDeptByLevel() {
        Map<String, String> deptMap = new HashedMap();
        List<HisDeptLevel> hisDeptLevelList = IHisDeptLevelService.getAll();
        if (!hisDeptLevelList.isEmpty() && hisDeptLevelList != null) {
            deptMap = hisDeptLevelList.stream().collect(Collectors.toMap(hisDeptLevel -> hisDeptLevel.getDepartmentCode(), HisDeptLevel::getWardareaCode));
        }
        return deptMap;
    }
}
